C#

您所在的位置:网站首页 hash key C#

C#

#C# | 来源: 网络整理| 查看: 265

Hashtable.GetHash(Object) method is used to get the hashcode of the specified key of a Hashtable object. This method is inherited from the Object Class.

Syntax:

protected virtual int GetHash(Object Key);

Exception: This method will give NullReferenceException if the key is null.

Below programs illustrate the use of above-discussed method:

Example 1:

// C# Program to illustrate the // Hashtable.GetHash(Object) methodusing System;using System.Collections;  // Inheriting Hashtable as // Hashtable.GetHash(Object) // method is protected methodclass HashCode : Hashtable {          // Main Method    static void Main(string[] args)    {                  // creating object for HashCode as        // to access protected methods we        // have to create object for the         // derived class        HashCode h = new HashCode();                  // Add Elements into Hashtable        h.Add("1001", "Parek Shetty");        h.Add("1002", "Deshmuk Narayan");        h.Add("1003", "Ratan Kaalikaran");                  ICollection Key = h.Keys;          foreach(string val in Key)        {                          // printing Hashtable            Console.Write(val + " : " + h[val]);            Console.Write("\n");                          // printing hashcode with keys            int hcode = h.GetHash(val);                          Console.Write(val + " : " + hcode);            Console.Write("\n");        }    }}Output: 1002 : Deshmuk Narayan 1002 : 985757554 1001 : Parek Shetty 1001 : -1708895167 1003 : Ratan Kaalikaran 1003 : -1892225314

Example 2:

// C# Program to illustrate the // Hashtable.GetHash(Object) methodusing System;using System.Collections;  class HashCode : Hashtable {          // Main Method    static void Main(string[] args)    {        HashCode h = new HashCode();                  // Adding elements        h.Add('A', "Pritam Devadhya");        h.Add('B', "Arjun Balachi");        h.Add('C', "Timanad Panigrahi");                  ICollection Key = h.Keys;                  int hcode = h.GetHash('C');                  Console.Write("HashCode: " + hcode);    }}Output: HashCode: 4390979

Reference:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable.gethash?view=netframework-4.7.2My Personal Notes arrow_drop_up


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3